package com.neuedu.controller;

import com.alipay.api.AlipayApiException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.neuedu.entity.PmsOrder;
import com.neuedu.service.PmsOrderService;
import com.neuedu.vo.ResultJson;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;
import java.util.List;

/**
 * <p>
 * 订单表 前端控制器
 * </p>
 *
 * @author 王禹
 * @since 2025-08-13
 */
@RestController
@RequestMapping("/pmsOrder")
public class PmsOrderController {
    @Resource
    PmsOrderService pmsOrderService;
    @GetMapping("/getOrder")
    ResultJson<List<PmsOrder>> getOrder(String registerNo, LocalDate createDate) {
        return ResultJson.success(pmsOrderService.getOrder(registerNo, createDate));
    }

    @PostMapping("/updatePay")
    ResultJson<Boolean> updatePay(HttpServletRequest request) throws AlipayApiException {
        return ResultJson.success(pmsOrderService.updatePay(request));
    }
    @PostMapping("/ali/notify")
    String aliNotify(HttpServletRequest request) throws AlipayApiException {
        try {
            if (pmsOrderService.updatePay(request)) {
                return "success";
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "fail";
    }

    @PostMapping("/payment")
    ResultJson<String> payment(String no, String total, String type) throws AlipayApiException, JsonProcessingException {
        return ResultJson.success(pmsOrderService.payment(no, total, type));
    }
}
